package com.itgaoshu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.bean.SysRole;
import com.itgaoshu.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * 角色管理
 * @author: xiyue
 * Date: 2021/3/22
 * Time: 10:45
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private SysRoleService roleService;
    //查询所有的角色
    @RequestMapping("/loadAllRole")
    @ResponseBody
    public Object selectAllRoles(SysRole role, Integer page, Integer limit){
        System.out.println(role);
        PageHelper.startPage(page,limit);
        List<SysRole>  roles = roleService.selectAllRoles(role);
        PageInfo pageInfo = new PageInfo(roles);
        Map<String,Object> maps= new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        maps.put("code", 0);
        maps.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        maps.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        maps.put("data", pageInfo.getList());
        return maps;
    }
    //查询所有的角色
    @RequestMapping("/updateRole")
    @ResponseBody
    public  Object updateRole(SysRole role){
        int result = roleService.updateRole(role);
        if(result>0){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }

    //修改角色
    @RequestMapping("/insertRole")
    @ResponseBody
    public  Object addRole(SysRole role){
        int result = roleService.addRole(role);
        role.setRoleid(null);
        if(result>0){
            return "添加成功";
        }else{
            return "添加失败";
        }
    }

    @RequestMapping("/deleteRole")
    @ResponseBody
    public Object deleteRole(Integer roleid){
        int result = roleService.deleteRole(roleid);
        if(result>0){
            return  "删除成功";
        }else{
            return  "删除失败";
        }
    }
}
